//
//  KRCateListController.m
//  cookBook
//
//  Created by kuroken on 16/1/29.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRCateListController.h"
#import <MJExtension.h>
#import <SVProgressHUD.h>
#import "KRHttpRecipeTool.h"
#import "KRConst.h"
#import "KRCateList.h"
#import "KRCateViewCell.h"
#import "KRNoNetCell.h"
#import "KRCateTagController.h"
#import "KRCateTagsListController.h"

@interface KRCateListController () <KRCateViewCellDelegate>

/** KRCateList模型数组 */
@property (nonatomic, strong) NSArray<KRCateList *> *cateListArray;
/** 当前请求 */
@property (nonatomic, weak) NSURLSessionDataTask *currentDataTask;
@property (nonatomic, assign, getter=isError) BOOL error;
@end

@implementation KRCateListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分类";
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self loadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!self.cateListArray) {
        // 显示指示器
        [SVProgressHUD show];
        self.tableView.userInteractionEnabled = NO;
        self.tableView.scrollEnabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.currentDataTask cancel];
    self.currentDataTask = nil;
    [SVProgressHUD dismiss];
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
}

- (void)loadData
{
    self.currentDataTask = [KRHttpRecipeTool getParams:^(NSString *__autoreleasing *method, NSMutableDictionary *requestParams) {
        *method = KRSearchGetCatelist;
    } success:^(NSURLSessionDataTask *task, id json) {
        if (self.currentDataTask != task) return;

        self.error = NO;
        self.tableView.scrollEnabled = YES;
        [SVProgressHUD dismiss];
        self.tableView.userInteractionEnabled = YES;
        
        self.cateListArray = [KRCateList mj_objectArrayWithKeyValuesArray:json[@"result"][@"list"]];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (self.currentDataTask != task) return;

        self.error = YES;
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
        self.tableView.userInteractionEnabled = YES;
        KRLog(@"%@, %@", self, error);
    }];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.isError) return 1; // 请求失败
    if (!self.cateListArray.count) return 0;
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.isError) return 1; // 请求失败
    return self.cateListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isError) {
        KRNoNetCell *cell = [KRNoNetCell cellWithTableView:tableView];
        [cell setNoNetHint:@"网速不给力,无法加载"];
        return cell;
    }
    
    KRCateViewCell *cell = [KRCateViewCell cellWithTableView:tableView];
    cell.delegate = self;
    cell.cateList = self.cateListArray[indexPath.row];
    
    return cell;
}

#pragma mark - KRCateViewCellDelegate
- (void)cateViewCellDidClickCateBtnTagID:(NSInteger)tagID tagName:(NSString *)tagName
{
    KRCateTagController *cateTagVc = [[KRCateTagController alloc] init];
    cateTagVc.tagID = [NSString stringWithFormat:@"%d", (int)tagID];
    cateTagVc.title = tagName;
    [self.navigationController pushViewController:cateTagVc animated:YES];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isError) return [UIScreen mainScreen].bounds.size.height - KRTopBarHeight;

    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isError) {
        self.error = NO;
        [self.tableView reloadData];
        [self loadData];
        return;
    }

    KRCateTagsListController *tagsListVc = [[KRCateTagsListController alloc] init];
    tagsListVc.cateList = self.cateListArray[indexPath.row];
    [self.navigationController pushViewController:tagsListVc animated:YES];
}

@end